package cn.sunny360.messsage.impl.json;

import cn.sunny360.messsage.MsgDevice;
import com.alibaba.fastjson.JSONObject;

import java.nio.charset.StandardCharsets;

public class JsonMsgDevice implements MsgDevice<JsonMsgDevice> {
    private JSONObject content;

    public JsonMsgDevice(){
        content = new JSONObject();
    }

    public JsonMsgDevice(String json){
        content = JSONObject.parseObject(json);
    }

    public JsonMsgDevice(JSONObject jsonObject) {
        content = jsonObject;
    }

    @Override
    public String toJson() {
        return content.toJSONString();
    }

    @Override
    public JsonMsgDevice parseJson(String s) {
        content = JSONObject.parseObject(s);
        return this;
    }

    @Override
    public byte[] toByteArray() {
        return content.toJSONString().getBytes(StandardCharsets.UTF_8);
    }

    @Override
    public JsonMsgDevice parseByteArray(byte[] bytes) {
        content = JSONObject.parseObject(new String(bytes, StandardCharsets.UTF_8));
        return this;
    }


    @Override
    public String getDeviceId() {
        return content.getString("device_id");
    }

    @Override
    public String getDeviceBrand() {
        return content.getString("device_brand");
    }

    @Override
    public String getDeviceType() {
        return content.getString("device_type");
    }

    @Override
    public String getOs() {
        return content.getString("os");
    }

    @Override
    public String getOsVerison() {
        return content.getString("os_version");
    }

    @Override
    public String getIosToken() {
        return content.getString("ios_token");
    }

    @Override
    public String getAppVer() {
        return content.getString("app_ver");
    }

    @Override
    public MsgDevice setDeviceId(String deviceId) {
        content.put("device_id", deviceId);
        return this;
    }

    @Override
    public MsgDevice setDeviceBrand(String deviceBrand) {
        content.put("device_brand", deviceBrand);
        return this;
    }

    @Override
    public MsgDevice setDeviceType(String deviceType) {
        content.put("device_type", deviceType);
        return this;
    }

    @Override
    public MsgDevice setOs(String os) {
        content.put("os", os);
        return this;
    }

    @Override
    public MsgDevice setOsVerison(String osVerison) {
        content.put("os_version", osVerison);
        return this;
    }

    @Override
    public MsgDevice setIosToken(String iosToken) {
        content.put("ios_token", iosToken);
        return this;
    }

    @Override
    public MsgDevice setAppVer(String appVer) {
        content.put("app_ver", appVer);
        return this;
    }
}
